/**********************************************
** @Author: phy
** @Date:   2017-12-12 14:17:37
** @Last Modified by:   phy
***********************************************/
package models

import (
	"github.com/astaxie/beego/orm"
	"errors"
	"fmt"
)

type RoomCheatMain struct {
	Rid        				int64 `orm:"pk;auto"`
	RoomID     				int     //'房间ID',
	RoomName   				string  //'房间名',
	KindID     				int     //'游戏类型',
	AllWinLose  			int64   //'房间历史总输赢',
	UserCheatBloodPool      int64   //'玩家作弊血池值',
	AllUserCheatBloodPool   int64   //'玩家电控血池总值',
	RoomBloodPool       	int64   //'房间当前血池',
	RoomBloodBalance		int64   //'房间血池平衡值',
	RoomAwardPool     		int64   //'房间当前奖池',
	MinusCheatRate		    int     //'房间负分作弊率',
	EatCheatRate			int     //'房间吃分作弊率',
	VomitCheatRate		    int     //'房间吐分作弊率',
	TaxRate					int     //'房间税率',
	RobotCount				int     //'机器人数量',
	AllowEnter				int     //'0-允许进入  1-不允许进入',
}

type RoomCheatSub struct{
	Rid        				int64 `orm:"pk;auto"`
	RoomID     				int    //'房间ID',
	TodayWinLose			int64  //'房间当日输赢',
	UpdateTime				int64  //'更新时间',
	UserCheatBloodPool 		int64  //'玩家作弊血池值',
	TodayVomitScore			int64  //'当日吐分量',
	TodayEatScore			int64  //'当日吃分量',
	TodayVomitTimes			int64  //'当日吐分次数'
	RoomState				int    //'房间当前吃分吐分状态 1-吃分 2吐分',
	TodayEatTimes			int    //'当日吃分次数',
}

type RoomBloodOperate struct {
  	Rid   					int64 `orm:"pk;auto"`
  	RoomID 					int
  	BloodScore				int64 //'血池变化值 大于0添加血池系统放水， 小于0系统吃分',
  	OperateTime				int64 //'执行时间点',
  	Status 					int   //'状态 0-未执行 1-已执行',
  	AddGid					int   // '添加管理员ID',
  	DelGid 					int   //'删除管理员ID',
  	DelTime 				string //'删除时间',
    OpTime 					int64 // '操作时间',
}
type RoomCtrlInfo struct {
  ID 						int64 `orm:"pk;auto"`
  ServerID 					int		//'房间ID',
  NType 					int 	//'控制类型  0-房间作弊率等控制  1-房间是否允许进入',
  RoomBloodBalance 			int64	//T '房间血池平衡值',
  MinusCheatRate 			int     //'房间负分作弊率',
  EatCheatRate				int     //'房间吃分作弊率',
  VomitCheatRate			int     //'房间吐分作弊率',
  TaxRate 					int     //'房间税率',
  RobotCount 				int     //'机器人数量',
  AllowEnter 				int 	//'是否允许进入 0-仅需 1-不允许',
  
}
type RoomBloodChange struct{
	 ID 						int64 `orm:"pk;auto"`
	 ServerID					int   `orm:"index"`
	 WriteDateTime				int64 `orm:"index"`
	 RoomBloodBalance			int64
	 BloodScore					int64
}

func (a *RoomCheatMain) TableName() string {
	return TableName("RoomCheatMain")
}

func  RoomCheatMainGetByRoomID(iRoomID int) (*RoomCheatMain, error) {

	o := orm.NewOrm()
	rooInfom := RoomCheatMain{RoomID: iRoomID}

	err := o.Read(&rooInfom,"RoomID")

	if err == orm.ErrNoRows {
 	   return &rooInfom, err
	} else if err == orm.ErrMissPK {
     	return &rooInfom, err
	} else {
		return &rooInfom, errors.New("无结果！")
    }
	
}

func  RoomCheatMainGetList(page, pageSize int, filters ...interface{}) ([]*RoomCheatMain, int64) {
	offset := (page - 1) * pageSize
	list := make([]*RoomCheatMain, 0)
	query := orm.NewOrm().QueryTable(TableName("RoomCheatMain"))
	if len(filters) > 0 {
		l := len(filters)
		for k := 0; k < l; k += 2 {
			query = query.Filter(filters[k].(string), filters[k+1])
		}
	}
	total, _ := query.Count()
	query.OrderBy("KindID").Limit(pageSize, offset).All(&list)

	return list, total
}



func  RoomCheatMainGetAllList(filters ...interface{}) ([]*RoomCheatMain, int64) {

	list := make([]*RoomCheatMain, 0)
	query := orm.NewOrm().QueryTable(TableName("RoomCheatMain"))
	if len(filters) > 0 {
		l := len(filters)
		for k := 0; k < l; k += 2 {
			query = query.Filter(filters[k].(string), filters[k+1])
		}
	}
	//total, _ := query.Count()
	query.OrderBy("KindID").Limit(-1).All(&list)

	return list, 0
}


func (a *RoomCheatMain)Update(iRoomID int,fields ...string) error{
	o := orm.NewOrm()
	rooInfom := RoomCheatMain{RoomID: iRoomID}
	err := o.Read(&rooInfom,"RoomID")
	if err != nil {
		return err
	}
	a.Rid = rooInfom.Rid;
	_, err = o.Update(a, fields...); 
	return err

}
func  RoomCheatSubTodayInfo(DayTzeroTime int64,sys_today_Win string) ([]*RoomCheatSub, []int) {

//	o := orm.NewOrm()
//	getData := RoomCheatSub{RoomID: id,UpdateTime:DayTzeroTime}

//	err := o.Read(&getData,"RoomID","UpdateTime")

//	if err == orm.ErrNoRows {
 //	   return &getData, err
//	} else if err == orm.ErrMissPK {  
  //   	return &getData, err
//	} else {
  //      return &getData, err
 //  	}
//	return &getData, errors.New("无结果！")
	list := make([]*RoomCheatSub, 0)
	query := orm.NewOrm().QueryTable(TableName("RoomCheatSub"))
	if  sys_today_Win == "0"{
		query = query.Filter("TodayWinLose__lt",0)
	}else if sys_today_Win == "1"{
		query = query.Filter("TodayWinLose__gt",0)	
	}
	query = query.Filter("UpdateTime",DayTzeroTime)
	query.All(&list)

	//total, _ := query.Count()
	roomIDs := make([]int, (len(list)))
	for k, v := range list {
		roomIDs[k] = v.RoomID
	}
	return list, roomIDs
}
func RoomCheatSubGetTodayDataByRoomID(data []*RoomCheatSub,iRoomID int)(int64,int64){
	var TodayWin,TodayLose int64
	for _, v := range data {
		if v.RoomID == iRoomID{
			TodayWin = v.TodayWinLose
			TodayLose = v.TodayEatScore
			//fmt.Println("----",TodayWin,TodayLose,iRoomID)
		}
	}
	//fmt.Println(TodayWin,TodayLose,iRoomID)
	return TodayWin,TodayLose
}

func  RoomCheatSubGetList(page, pageSize int, filters ...interface{}) ([]*RoomCheatSub, int64) {
	offset := (page - 1) * pageSize
	list := make([]*RoomCheatSub, 0)
	query := orm.NewOrm().QueryTable(TableName("RoomCheatSub"))
	if len(filters) > 0 {
		l := len(filters)
		for k := 0; k < l; k += 2 {
			query = query.Filter(filters[k].(string), filters[k+1])
		}
	}
	total, _ := query.Count()
	query.OrderBy("-UpdateTime").Limit(pageSize, offset).All(&list)

	return list, total
}
// func RoleAuthDelete(id int) (int64, error) {
// 	query := orm.NewOrm().QueryTable(TableName("role_auth"))
// 	return query.Filter("role_id", id).Delete()
// }

// func RoleAuthMultiAdd(ras []*RoleAuth) (n int, err error) {
// 	query := orm.NewOrm().QueryTable(TableName("role_auth"))
// 	i, _ := query.PrepareInsert()
// 	for _, ra := range ras {
// 		_, err := i.Insert(ra)
// 		if err == nil {
// 			n = n + 1
// 		}
// 	}
// 	i.Close() // 别忘记关闭 statement
// 	return n, err
// }

func (a *RoomBloodOperate) Insert() error {

	_,err := orm.NewOrm().Insert(a)
	return err
}

func (a *RoomBloodOperate) Update(fields ...string) error {
	_, err := orm.NewOrm().Update(a, fields...); 
	return err;
}

func  RoomBloodOperateGetList(page, pageSize int, filters ...interface{}) ([]*RoomBloodOperate, int64) {
	offset := (page - 1) * pageSize
	list := make([]*RoomBloodOperate, 0)
	query := orm.NewOrm().QueryTable(TableName("RoomBloodOperate"))
	if len(filters) > 0 {

		l := len(filters)
		for k := 0; k < l; k += 2 {
			query = query.Filter(filters[k].(string), filters[k+1])
		}
	}
	total, _ := query.Count()
	query.OrderBy("OperateTime").Limit(pageSize, offset).All(&list)

	return list, total
}
func  RoomBloodOperateXLSdata(rows *[]RoomBloodOperate){
	

	o:= orm.NewOrm();
	query :=o.QueryTable(TableName("RoomBloodOperate"))
	i, _ := query.PrepareInsert()
	
    for _, row := range *rows {
		fmt.Println("rows====================>>",row)

		i.Insert(&row);
	}
	
	defer i.Close() // 别忘记关闭 statement

	
}
func  GetRoomNameFromRoomInfoByID(roominfo []*RoomCheatMain,roomID int) string {
	RoomName:= "未知"
	for _, v := range roominfo {
		if v.RoomID == roomID{
			RoomName = v.RoomName
		}
		
	}
	return RoomName
}

func (a *RoomCtrlInfo) Insert() error {

	_,err := orm.NewOrm().Insert(a)
	return err
}

func  RoomBloodChangeGetAllList(filters ...interface{}) ([]*RoomBloodChange, int64) {

	list := make([]*RoomBloodChange, 0)
	query := orm.NewOrm().QueryTable(TableName("RoomBloodChange"))
	if len(filters) > 0 {
		l := len(filters)
		for k := 0; k < l; k += 2 {
			query = query.Filter(filters[k].(string), filters[k+1])
		}
	}
	//total :=  0;//query.Count()
	 total := GetRoomBloodChangeMax()
	//作一个限制只弄两万条数据
	query.OrderBy("-WriteDateTime").Limit(total).All(&list)

	return list, total
}


